package com.usefullc.system.service.component;

import com.usefullc.common.enums.CheckCodeTypeEnum;
import com.usefullc.common.exception.BizException;
import com.usefullc.system.service.AbstractService;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

/**
 * @author: Sampson
 * @date: 2018-12-19 14:31
 */
@Component
public class SmsComponent  extends AbstractService {

    @Autowired
    private SystemConfigComponent systemConfigComponent;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    public String getImgCode(Integer type,final String areaCode,final String mobile){
        if (mobile.length() != 11 || !mobile.startsWith("1")) {
            throw new BizException("手机账号不合法");
        }
        final String smsCode = RandomStringUtils.random(4, false, true);

        if(!systemConfigComponent.isProd()){
            logger.info("smsCode="+smsCode);
        }
        //
        CheckCodeTypeEnum smsCodeTypeEnum = CheckCodeTypeEnum.getByCode(type);
        String smsCodeKey = StringUtils.join(new Object[]{smsCodeTypeEnum.getCacheKey(), areaCode + mobile}, "_");
        String cacheValue = stringRedisTemplate.opsForValue().get(smsCodeKey);
        if (StringUtils.isNotEmpty(cacheValue)) {  //存在不用重复发送
            return cacheValue;
        }
        logger.info("checkCodeKey={}",smsCodeKey);
        stringRedisTemplate.opsForValue().set(smsCodeKey,smsCode, smsCodeTypeEnum.getExpireTime(), smsCodeTypeEnum.getTimeUnit());
        if (systemConfigComponent.isProd()) {  //生产环境才发送验证码
            new Thread(() -> {
                String value = "%23code%23%3d"+smsCode;
                //TODO
//                SmsServer.this.sendMsgJH(AppConstant.SMSTemplate.registerId,mobile,value,0);
            }).start();
        }
        return smsCode;
    }

    public static void main(String[] args) {

//        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "<your-access-key-id>", "<your-access-key-secret>");
//        /** use STS Token
//         DefaultProfile profile = DefaultProfile.getProfile(
//         "<your-region-id>",           // The region ID
//         "<your-access-key-id>",       // The AccessKey ID of the RAM account
//         "<your-access-key-secret>",   // The AccessKey Secret of the RAM account
//         "<your-sts-token>");          // STS Token
//         **/
//
//        IAcsClient client = new DefaultAcsClient(profile);
//
//
//        SendSmsRequest request = new SendSmsRequest();
//
//        try {
//            SendSmsResponse response = client.getAcsResponse(request);
//            System.out.println(new Gson().toJson(response));
//        } catch (ServerException e) {
//            e.printStackTrace();
//        } catch (ClientException e) {
//            System.out.println("ErrCode:" + e.getErrCode());
//            System.out.println("ErrMsg:" + e.getErrMsg());
//            System.out.println("RequestId:" + e.getRequestId());
//        }

    }

}
